#pragma once 
#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include <vector>
#include<atomic>
#include<fstream>
#include<sys/time.h>
#include<boost/algorithm/string.hpp>
//#include<boost/algorithm/string.hpp>
using namespace boost;
using namespace std;
namespace ns_util
{
     class TimeUtil
    {
        public:
        TimeUtil()
        {}
        static std::string Gettimestamp()
        {
            struct timeval ta;
            gettimeofday(&ta,nullptr);
            return std::to_string(ta.tv_sec);
        }
        //获取毫秒级时间戳
        static std::string GettimeMS()
        {
            struct timeval ta;
            gettimeofday(&ta,nullptr);
            return std::to_string(ta.tv_sec*1000+ta.tv_usec/1000);
        }
        ~TimeUtil()
        {}
    };
    std::string tmp="./tmp/";
    class Pathutil
    {
        public:
        static std::string AddSuffix(const std::string&file_name,const std::string& suffix)
        {
            std::string file=tmp;
            file+=file_name;
            file+=suffix;
            return file;
            
        }
        Pathutil()
        {}
        //编译时需要的文件
        static std::string Src(const std::string& file_name)
        {
            return AddSuffix(file_name,".cpp");
        }
        static std::string Exe(const std::string& file_name)
        {
            return AddSuffix(file_name,".exe");
        }
        static std::string Compile_error(const std::string&file_name)
        {
                return AddSuffix(file_name,".compile_error");
        }
        //运行是需要的文件
        static std::string Stdin(const std::string&file_name)
        {
            return AddSuffix(file_name,".stdin");
        }
        static std::string Stdout(const std::string&file_name)
        {
            return AddSuffix(file_name,"stdout");
        }
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name,".stderr");
        }
        ~Pathutil()
        {}
    };
    class Fileutil
    {
        public:
        static bool isExefileEeist(std::string file)
        {
            struct stat st;
            if(stat(file.c_str(),&st)==0)
            {
                return true;
            }
            return false;
        }
        //毫秒级时间戳+原子唯一性递增，确定文件的唯一性
        static std::string UniqFilename()
        {
            static std::atomic_int id(0);
            id++;
            std::string ms=TimeUtil::GettimeMS();
            std::string strid=std::to_string(id);

            return ms+"_"+strid;
        }
        static bool Writefile(const std::string&target,const std::string&code)
        {
            std::ofstream in(target.c_str());
            // if(!in.is_open());
            // {
            //     return false;
            // }
            in.is_open();
            in.write(code.c_str(),code.size());
            in.close();
            return true;
        }
        static bool Readfile(const std::string &target,std::string*comtent,bool keep)
        {
            (*comtent).clear();
            std::ifstream out(target);
            if(!out.is_open())
            {
                return false;
            }
            
            std::string line;
            //1、不保存"\n"
            //2、内部实现了强制类型转换
            while(getline(out,line))
            {
                (*comtent)+=line;
                (*comtent)+=(keep?"\n":"");
            }
            out.close();
            return true;
        }
    };
    class SplitUtil
    {
        public:
        static void StringSplit(std::string line,std::vector<std::string>*out,std::string sep)
        {
            boost::split((*out),line,boost::is_any_of(sep),boost::algorithm::token_compress_on);
        }
    };
}
